package com.wenhai.spring.dependency.injection;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author:wenhai
 * @Description: {@link org.springframework.beans.factory.Aware}接口回调注入
 * @Date:2020/3/9
 */
public class AwareInterfaceDependencyInjectionDemo implements BeanFactoryAware, ApplicationContextAware {
    private static BeanFactory beanFactory;
    private static ApplicationContext applicationContext;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext();
        cxt.register(AwareInterfaceDependencyInjectionDemo.class);
        cxt.refresh();
        System.out.println("beanFactory与application中的AnnotationConfigApplicationContext是否相等：" + (beanFactory == cxt));
        System.out.println("beanFactory与application中的beanFactory是否相等：" + (beanFactory == cxt.getBeanFactory()));
        System.out.println("beanFactory与applicationContext中的AnnotationConfigApplicationContext是否相等：" + (applicationContext == cxt));
        cxt.close();
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        AwareInterfaceDependencyInjectionDemo.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AwareInterfaceDependencyInjectionDemo.applicationContext = applicationContext;
    }
}
